import Taro from "@tarojs/taro";
export const baseURL = "https://mini.shxinling.com.cn";

const request = (url, method, data) => {
    return new Promise((resolve, reject) => {
        Taro.request({
            url:baseURL+url,
            method:method || "GET",
            data:data || null,
            success:async res => {
                if ([0, 200].includes(res.data.code)) {
                    resolve(res.data.data)
                }else if (res.data.code === 525){
                    await Taro.showToast({
                        title:"登录过期，请重新登录",
                        icon:"none"
                    })
                    Taro.hideLoading();
                    Taro.clearStorageSync();
                    reject()
                    setTimeout(() => {
                        Taro.reLaunch({
                            url:"/pages/index/index"
                        })
                    },1000)
                }else {
                    await Taro.showToast({
                        title: res.data.msg || "网络错误",
                        icon: "none"
                    })
                    Taro.hideLoading();
                    reject()
                }
            },
            fail:err => {
                Taro.showToast({
                    title: "网络错误",
                    icon: "none"
                }).then(r => {})
                Taro.hideLoading();
                reject()
                Taro.clearStorageSync();
                setTimeout(() => {
                    Taro.reLaunch({
                        url:"/pages/index/index"
                    })
                },1000)
            }
        })
    })
}
const get = (url,data) => request(url,"GET",data);

const post = (url,data) => request(url,"POST",data);

export default { get, post }